package com.kly.chatter.api.application.controller.sys;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.fqgj.common.api.Response;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.chatter.api.application.config.AppVersionInfoConfig;
import com.kly.chatter.api.application.controller.chatter.req.UupgradationReq;
import com.kly.chatter.api.application.controller.sys.req.MessageReadRequest;
import com.kly.chatter.api.application.controller.sys.req.NoticeRequest;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.user.chatterService.ModelService;
import com.kly.user.dto.AppInfoDto;
import com.kly.user.dto.ReadMessageDto;
import com.kly.user.dto.ReadMessageReq;
import com.kly.user.dto.SysMessageDto;
import com.kly.user.dto.chatter.response.ModelsProfileDto;
import com.kly.user.service.OtherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author linqu
 */
@Api(value = "其它", tags = {"其它"})
@RestController
@RequestMapping("/api/sys")
public class SysController {

    private static final Log logger = LogFactory.getLog(SysController.class);
    @Resource
    ModelService modelService;
    @Resource
    OtherService otherService;

    @Autowired
    AppVersionInfoConfig appVersionInfoConfig;


    /**
     * model列表接口
     */
    @ApiOperation("model列表接口")
    @RequestMapping(value = "/modelList", method = {RequestMethod.POST})
    @SentinelResource
    public Response modelList() {
        String chatUserCode = RequestLocalInfo.getCurrentUser().getUserCode();
        final ModelsProfileDto dto = RpcUtils.getResult(modelService.queryModelDetails(chatUserCode));

        return Response.ok(dto);
    }


    /**
     * 系统消息
     */
    @ApiOperation("系统消息")
    @RequestMapping(value = "/notice", method = {RequestMethod.POST})
    @SentinelResource
    public Response notice() {
        String chatUserCode = RequestLocalInfo.getCurrentUser().getUserCode();
        final SysMessageDto result = RpcUtils.getResult(otherService.messageList(chatUserCode));
        return Response.ok(result);
    }

    /**
     * 系统消息
     */
    @ApiOperation("消息读取")
    @RequestMapping(value = "/notice/read", method = {RequestMethod.POST})
    @SentinelResource
    public Response read(@RequestBody MessageReadRequest request) {

        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        final List<String> readMessages = request.getReadMessages();
        final List<ReadMessageDto> hasReadMessages = readMessages.parallelStream().map(s -> ReadMessageDto.builder().msgCode(s).userCode(userCode).build()).collect(Collectors.toList());

        final ReadMessageReq messageReq = ReadMessageReq.builder().readMessages(hasReadMessages).build();
        return otherService.readMessage(messageReq);
    }

    @ApiOperation("系统升级")
    @RequestMapping(value = "/upgradation", method = {RequestMethod.POST})
    @SentinelResource
    public Response upgradation(@Valid @RequestBody UupgradationReq request, BindingResult results) {
        if (results.hasErrors()) {
            return Response.error();
        }
        AppInfoDto appInfoDto = new AppInfoDto();
        Map<String, String> chatAPP = appVersionInfoConfig.getAppConfig();
        appInfoDto.setAppCode(request.getAppCode());
        if (Integer.parseInt(chatAPP.get("versionCode")) > request.getVersionCode()) {
            appInfoDto.setUpdateFlag(true);
            appInfoDto.setUpdateType(1);
            appInfoDto.setAppStoreUrl(chatAPP.get("storeUrl"));
            appInfoDto.setOtherPlatforms(chatAPP.get("otherPlatforms"));
            appInfoDto.setUpGradationDesc(chatAPP.get("upGradationDesc"));
        } else {
            appInfoDto.setUpdateFlag(false);
        }
        return Response.ok(appInfoDto);
    }
}
